<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="收件人" prop="recipient">
        <el-input v-model="queryParams.recipient" placeholder="请输入收件人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="地址" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
        <el-input
          v-model="queryParams.isDelete"
          placeholder="请输入逻辑删除;0=未删除；1=已删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="收货省份" prop="receiverState">
        <el-input v-model="queryParams.receiverState" placeholder="请输入收货省份" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收货市" prop="receiverCity">
        <el-input v-model="queryParams.receiverCity" placeholder="请输入收货市" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收货区/街道" prop="receiverDistrict">
        <el-input v-model="queryParams.receiverDistrict" placeholder="请输入收货区/街道" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="详细地址" prop="detailAddress">
        <el-input v-model="queryParams.detailAddress" placeholder="请输入详细地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['product:internal_purchase:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['product:internal_purchase:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['product:internal_purchase:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['product:internal_purchase:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" plain @click="handleisopenSample" v-hasPermi="['product:apply:addApply']">发起内购</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" plain @click="handleisopenNote"
          v-hasPermi="['product:apply:addEditApply']">可编辑发起内购</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="internal_purchaseList" @selection-change="handleSelectionChange"
      @cell-click="tableEdit" @row-dblclick="t">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="联系ID" align="center" prop="id" />
      <el-table-column label="收件人" align="center" prop="recipient" />
      <el-table-column label="电话" align="center" prop="phone" />

      <!-- <el-table-column
        label="逻辑删除;0=未删除；1=已删除"
        align="center"
        prop="isDelete"
      /> -->

      <!-- <el-table-column label="收货省份" align="center" prop="receiverState" />
      <el-table-column label="收货市" align="center" prop="receiverCity" />
      <el-table-column
        label="收货区/街道"
        align="center"
        prop="receiverDistrict"
      /> 
    <el-table-column label="地址" align="center" prop="address" />
    -->
      <el-table-column width="500" label="详细地址" align="center" prop="detailAddress" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['product:internal_purchase:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['product:internal_purchase:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改 达人信息-联系方式对话框 :rules="rules"-->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" :inline="true">
        <el-form-item label="收件人" prop="recipient">
          <el-input v-model="form.recipient" placeholder="请输入收件人" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <!--  -->
        <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
          <el-input
            v-model="form.isDelete"
            placeholder="请输入逻辑删除;0=未删除；1=已删除"
          />
        </el-form-item> -->

        <!-- <el-form-item label="收货省份" prop="receiverState">
          <el-input v-model="form.receiverState" placeholder="请输入收货省份" />
        </el-form-item>
        <el-form-item label="收货市" prop="receiverCity">
          <el-input v-model="form.receiverCity" placeholder="请输入收货市" />
        </el-form-item>
        <el-form-item label="收货区/街道" prop="receiverDistrict">
          <el-input
            v-model="form.receiverDistrict"
            placeholder="请输入收货区/街道"
          />
        </el-form-item> -->

        <el-form-item label="所在区域" prop="">
          <el-row>
            <!-- <el-select v-model="form.receiverState" placeholder="请选择" @change="selectProvimce" style="width:130px">
              <el-option v-for="(item, index) of provincearr" :key="index" :label="item.name"
                :value="item.name"></el-option>
            </el-select>
            <el-select v-model="form.receiverCity" placeholder="请选择" @change="selectcity" style="width:130px">
              <el-option v-for="(item, index) of cityarr" :key="index" :label="item.name" :value="item.name"></el-option>
            </el-select>
            <el-select placeholder="请选择" v-model="form.receiverDistrict" style="width:150px">
              <el-option v-for="(item, index) of regionarr" :key="index" :label="item.name"
                :value="item.name"></el-option>
            </el-select> -->
            <el-select v-model="form.receiverState" clearable placeholder="省" @change="seletekey($event, 'province')"
              style="width: 130px">
              <el-option v-for="(val, key) in provinces" :key="key" :label="val" :value="val" />
            </el-select>
            <el-select v-model="form.receiverCity" clearable placeholder="市" @change="seletekey($event, 'city')"
              style="width: 130px">
              <el-option v-for="(val, key) in citys" :key="key" :label="val" :value="val" />
            </el-select>
            <el-select v-model="form.receiverDistrict" clearable placeholder="收货区/街道" @change="seletekey($event, 'area')"
              style="width: 150px" filterable allow-create>
              <el-option v-for="(val, key) in areas" :key="key" :label="val" :value="val" />
            </el-select>
          </el-row>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <!-- <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="form.detailAddress" placeholder="请输入详细地址" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="文本粘贴" prop="">
          <el-input v-model="demo" type="textarea" :autosize="{ minRows: 2, maxRows: 10 }"
            placeholder="省市区地址、姓名、电话11位用空格分开!!" style="width: 350px"></el-input>
          <el-button type="primary" size="mini" @click="addText">识别</el-button>
          <!-- <el-input v-model="sampleTalentPay.platform" placeholder="请输入平台名称" /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改产品二级信息对话框 :on-change="handleChange"-->
    <el-dialog title="内购申请" :visible.sync="isopenSample" append-to-body width="90%" v-if="isopenSample">
      <Ceshi :contactId="contactId" />
    </el-dialog>

    <!-- 添加或修改产品二级信息对话框 :on-change="handleChange"-->
    <el-dialog title="内购申请" :visible.sync="isopenNote" append-to-body width="90%" v-if="isopenNote">
      <Note :contactId="contactId" />
    </el-dialog>
  </div>
</template>

<script>
import AddressParse from "address-parse";
//数据获取
import { pcaa } from "area-data";
import axios from "axios";
import {
  listInternal_purchase,
  getInternal_purchase,
  delInternal_purchase,
  addInternal_purchase,
  updateInternal_purchase,
} from "@/api/internal/internal_purchase";
import Ceshi from "@/views/internal/Ceshi";
import Note from "@/views/internal/Note";
export default {
  name: "Internal_purchase",
  components: {
    Ceshi,
    Note,
  },
  data() {
    return {
      isopenNote: false,
      //文本粘贴
      demo: "",
      //省市区
      pcaaData: pcaa,
      // 区域数据
      provinces: pcaa["86"],
      citys: [],
      areas: [],

      contactId: "",
      isopenSample: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //  达人信息-联系方式表格数据
      internal_purchaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: null,
        recipient: null,
        address: null,
        isDelete: null,
        receiverState: null,
        receiverCity: null,
        receiverDistrict: null,
        detailAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [{ required: true, message: "电话不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleisopenSample() {
      if (this.ids.length < 1) {
        this.$modal.msgError("请选择一个地址发起内购");
      } else if (this.ids.length > 1) {
        this.$modal.msgError("只能勾选一个地址发起内购。");
      } else if (this.ids.length == 1) {
        this.isopenSample = true;
      }
    },
    handleisopenNote() {
      if (this.ids.length < 1) {
        this.$modal.msgError("请选择一个地址发起内购");
      } else if (this.ids.length > 1) {
        this.$modal.msgError("只能勾选一个地址发起内购。");
      } else if (this.ids.length == 1) {
        this.isopenNote = true;
      }
    },
    /** 查询 达人信息-联系方式列表 */
    getList() {
      this.loading = true;
      listInternal_purchase(this.queryParams).then((response) => {
        this.internal_purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phone: null,
        recipient: null,
        address: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        receiverState: null,
        receiverCity: null,
        receiverDistrict: null,
        detailAddress: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    tableEdit(row, column, cell, event) {
      console.log(JSON.stringify(row.id));
      this.contactId = row.id.toString();
    },
    //点击某一行
    t(row, column, cell, event) {
      this.handleUpdate(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.contactId = this.ids[0].toString();
      console.log(this.contactId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加 达人信息-联系方式";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInternal_purchase(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改 达人信息-联系方式";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.demo = "";
      this.form.detailAddress = `${this.form.receiverState}${this.form.receiverCity}${this.form.receiverDistrict}${this.form.address}`;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInternal_purchase(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInternal_purchase(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除 达人信息-联系方式编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInternal_purchase(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "product/internal_purchase/export",
        {
          ...this.queryParams,
        },
        `internal_purchase_${new Date().getTime()}.xlsx`
      );
    },
    addText() {
      if (!this.demo) {
        this.$modal.msgError("请输入内容");
        return;
      }
      let address = this.demo.replace(/\s+/g, " ");
      const result = AddressParse.parse(address);

      this.form.phone = result[0].mobile ? result[0].mobile : "";
      this.form.recipient = result[0].name ? result[0].name : "";
      this.form.receiverState = result[0].province ? result[0].province : "";
      this.form.receiverCity = result[0].city ? result[0].city : "";
      this.form.receiverDistrict = result[0].area ? result[0].area : "";
      this.form.address = result[0].details;
      // this.form.detailAddress = `${result[0].province}${result[0].city}${result[0].area}${result[0].details}`;

      this.seletekey(this.form.receiverState, "province");
      this.seletekey(this.form.receiverCity, "city");
      this.seletekey(this.form.receiverDistrict, "area");

      let obj = {
        fullAddress: this.demo,
        province: this.form.receiverState,
        city: this.form.receiverCity,
        district: this.form.receiverDistrict,
        detailedAddress: this.form.address,

        recipientPhone: this.form.phone,
        recipientName: this.form.recipient,
      };

      axios({
        method: "post",
        url: "http://47.107.129.160:9001/prod-api/product/parsing",
        // url: "http://47.97.123.182:1399/smAddress",
        data: obj,
      }).then((response) => { });

      // axios({
      //   method: "post",
      //   url: "https://wangzc.wang/smAddress",
      //   // url: "http://47.97.123.182:1399/smAddress",
      //   data: {
      //     address: address,
      //   },
      // }).then((res) => {
      //   //   // console.log(JSON.stringify(res.data));
      //   //   this.form.phone = res.data.phone ? res.data.phone : "";
      //   //   this.form.recipient = res.data.name ? res.data.name : "";
      //   //   this.form.receiverState = res.data.province ? res.data.province : "";
      //   //   this.form.receiverCity = res.data.city ? res.data.city : "";
      //   //   this.form.receiverDistrict = res.data.county ? res.data.county : "";
      //   //   this.form.address = res.data.street
      //   //     ? res.data.province +
      //   //       res.data.city +
      //   //       res.data.county +
      //   //       res.data.street +
      //   //       res.data.address
      //   //     : res.data.address;
      //   //   this.form.detailAddress = res.data.street
      //   //     ? res.data.street + res.data.address
      //   //     : res.data.address;
      //   //   this.seletekey(this.form.receiverState, "province");
      //   //   this.seletekey(this.form.receiverCity, "city");
      //   //   this.seletekey(this.form.receiverDistrict, "area");
      // });
    },
    seletekey(e, type) {
      if (e) {
        switch (type) {
          case "province":
            Object.keys(this.provinces).filter((item) => {
              if (this.provinces[item] === e) {
                this.citys = this.pcaaData[item];
              }
            });
            break;
          case "city":
            Object.keys(this.citys).filter((item) => {
              if (this.citys[item] === e) {
                this.areas = this.pcaaData[item];
              }
            });
            break;
          case "area":
            this.form.receiverDistrict = e;
            this.$forceUpdate();
            break;
        }
      }
    },
  },
};
</script>
185740322436359340
01194115393020836274
263711662621093062
1150380608-1728928025
5538164424629222
435760129
6664013424076454
01532741232024627769
3328149689
1957290610639419505
2840433430954742
202846621433423347
01253445620935414847
537827973
251316305737754928
02331715576421611049
582912370237754244